/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.fabianterhorst.isometric.slice;

import io.fabianterhorst.isometric.Constant;
import io.fabianterhorst.isometric.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;

import io.fabianterhorst.isometric.HiLogs;
import io.fabianterhorst.isometric.MainAbility;
import ohos.aafwk.content.Operation;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.app.Context;

import java.util.ArrayList;

/**
 * ListAbilitySlice.
 *
 * @since 2021-03-11
 */
public class ListAbilitySlice extends AbilitySlice {
    /**
     * To get class name
     */
    private static final String TAG = ListAbilitySlice.class.getName();

    ListContainer itemList;

    public static final String LIST_ITEM = "List_Items";

    /**
     * callback
     *
     * @param intent {@link Intent}
     */
    @Override
    public void onStart(Intent intent) {

        super.onStart(intent);

        ComponentContainer container = (ComponentContainer) LayoutScatter.getInstance(this)
            .parse(ResourceTable.Layout_Item_list_ability, null, false);

        itemList = (ListContainer) container.findComponentById(ResourceTable.Id_list_shapes);

        itemList.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {

                Operation operation = new Intent.OperationBuilder().withBundleName("io.fabianterhorst.isometric")
                    .withAbilityName(MainAbility.class.getSimpleName())
                    .build();
                Intent intent = new Intent();
                intent.setOperation(operation);
                intent.setParam(LIST_ITEM, listContainer.getItemProvider().getItem(i).toString());
                startAbility(intent);
            }
        });
        /**
         * Adding items in the list
         */
        ArrayList<String> items = new ArrayList<>();
        items.add(Constant.MULTIPLE_SHAPES);
        items.add(Constant.COMPLEX_SHAPE_1);
        items.add(Constant.COMPLEX_SHAPE_2);
        items.add(Constant.DRAW_COMPLEX_PRISM);
        items.add(Constant.DRAW_PRISM_1);
        items.add(Constant.DRAW_PRISM_2);
        items.add(Constant.DRAW_PRISM_3);
        items.add(Constant.DRAW_PRISM_4);
        items.add(Constant.DRAW_KNOT_1);
        items.add(Constant.DRAW_KNOT_2);
        items.add(Constant.DRAW_KNOT_3);
        items.add(Constant.DRAW_CYLINDER_1);
        items.add(Constant.DRAW_CYLINDER_2);
        items.add(Constant.DRAW_CYLINDER_3);
        items.add(Constant.DRAW_CYLINDER_4);
        items.add(Constant.DRAW_OCTAHEDRON_1);
        items.add(Constant.DRAW_OCTAHEDRON_2);
        items.add(Constant.DRAW_OCTAHEDRON_3);
        items.add(Constant.DRAW_PYRAMID_1);
        items.add(Constant.DRAW_PYRAMID_2);
        items.add(Constant.DRAW_PYRAMID_3);
        items.add(Constant.DRAW_PYRAMID_4);
        items.add(Constant.DRAW_PYRAMID_5);
        items.add(Constant.DRAW_STAIRS_1);
        items.add(Constant.DRAW_STAIRS_2);
        items.add(Constant.DRAW_STAIRS_3);

        MyProvider provider = new MyProvider(ListAbilitySlice.this, items);
        itemList.setItemProvider(provider);
        super.setUIContent(container);
    }

    /**
     * callback
     */
    @Override
    public void onActive() {
        super.onActive();
        HiLogs.e(TAG, "onForeground() : ");
    }

    /**
     * callback
     *
     * @param intent
     */
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
        HiLogs.e(TAG, "onForeground() : " + intent);
    }

    /**
     * MyProvider.
     *
     * @since 2021-03-28
     */
    private class MyProvider extends BaseItemProvider {
        private ArrayList<String> itemList;

        private Context context;

        /**
         * Constructor
         *
         * @param listAbilitySlice {@link ListAbilitySlice}
         * @param itemList {@link ArrayList}
         */
        public MyProvider(ListAbilitySlice listAbilitySlice, ArrayList<String> itemList) {
            this.itemList = itemList;
            this.context = listAbilitySlice;
        }

        /**
         * getCount of list size
         *
         * @return itemList
         */
        @Override
        public int getCount() {
            return itemList.size();
        }

        /**
         * getItem
         * @param i {@link int}
         * @return itemList
         */
        @Override
        public Object getItem(int i) {
            return itemList.get(i);
        }

        /**
         * getItemId
         * @param i {@link int}
         * @return i
         */
        @Override
        public long getItemId(int i) {
            return i;
        }

        /**
         * getComponent
         * @param i {@link int}
         * @param component {@link Component}
         * @param componentContainer {@link ComponentContainer}
         * @return convertView
         */
        @Override
        public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
            ComponentContainer convertView = (ComponentContainer) LayoutScatter.getInstance(context)
                .parse(ResourceTable.Layout_list_item, null, false);
            Text title = (Text) convertView.findComponentById(ResourceTable.Id_text_data);
            title.setText(getItem(i).toString());
            return convertView;
        }
    }

}
